<?php

namespace api\modules\v1\controllers\customer;

use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\dengta\robot\MessageService;
use Yii;

class WechatController extends OnAuthController
{
    public $modelClass = '';

    protected $optional = ['download-message'];

    /**
     * @api {get} /v1/customer/wechat/list 微信列表
     * @apiName list
     * @apiGroup Wechat
     *
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.page                      当前页码
     * @apiSuccess {Number}  data.pageSize                  每页显示数
     * @apiSuccess {Number}  data.total                     总数
     * @apiSuccess {Array}   data.list                      患者列表
     * @apiSuccess {Number}  data.list.id                   患者ID
     * @apiSuccess {Number}  data.list.cid                  项目ID
     * @apiSuccess {String}  data.list.wechatid             微信ID
     * @apiSuccess {String}  data.list.wechatno             微信号
     * @apiSuccess {String}  data.list.wechatnick           昵称
     * @apiSuccess {String}  data.list.avatar               头像
     * @apiSuccess {Number}  data.list.isonline             是否在线【0在线、1离线】
     * @apiSuccess {String}  data.list.isonline_str         对应值
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

//        $page = $params['page'] ?? 1;
//        $pageSize = $params['pageSize'] ?? 10;
        return CommonHelper::dengta_request('/v1/baiyou/wechat-list');
    }

    /**
     * @api {get} /v1/customer/wechat/contact-list 微信好友列表
     * @apiName contact-list
     * @apiGroup Wechat
     *
     * @apiParam {Number} wechat_id                         微信ID
     * @apiParam {String} date                              日期【格式：2020-01-01】
     * @apiParam {String} keywords                          关键词
     * @apiParam {Number} is_reply                          是否未回复【1是、0全部】
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.page                      当前页码
     * @apiSuccess {Number}  data.pageSize                  每页显示数
     * @apiSuccess {Number}  data.total                     总数
     * @apiSuccess {Array}   data.list                      患者列表
     * @apiSuccess {Number}  data.list.id                   患者ID
     * @apiSuccess {Number}  data.list.cid                  项目ID
     * @apiSuccess {String}  data.list.wechatid             微信ID
     * @apiSuccess {String}  data.list.friendid             好友ID
     * @apiSuccess {String}  data.list.friend_wechatno      好友微信号
     * @apiSuccess {String}  data.list.wechatnick           好友昵称
     * @apiSuccess {String}  data.list.avatar               好友头像
     * @apiSuccess {Number}  data.list.message_cnt          聊天数
     * @apiSuccess {String}  data.list.lats_create_time     最近聊天时间
     */
    public function actionContactList()
    {
        $params = Yii::$app->request->get();
        $params['wechat_id'] = $params['wechat_id'] ?? '';

        return CommonHelper::dengta_request('/v1/baiyou/contact-list', $params);
    }


    /**
     * @api {get} /v1/customer/wechat/message-list 微信好友聊天列表
     * @apiName message-list
     * @apiGroup Wechat
     *
     * @apiParam {String} wechat_id                         微信ID
     * @apiParam {String} friend_id                         好友微信ID
     * @apiParam {String} date                              日期【格式：2020-01-01,传type参数则失效】
     * @apiParam {String} keywords                          关键词
     * @apiParam {String} type                              翻页类型【first首次、prev上次、next下次、end尾次、all全部】
     * @apiParam {String} now_date                          当前日期【格式：2020-01-01，type=prev\next必传】
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.page                      当前页码
     * @apiSuccess {Number}  data.pageSize                  每页显示数
     * @apiSuccess {Number}  data.total                     总数
     * @apiSuccess {Array}   data.list                      患者列表
     * @apiSuccess {Number}  data.list.id                   患者ID
     * @apiSuccess {Number}  data.list.cid                  项目ID
     * @apiSuccess {String}  data.list.wechatid             微信ID
     * @apiSuccess {String}  data.list.friendid             好友ID
     * @apiSuccess {String}  data.list.friend_wechatno      好友微信号
     * @apiSuccess {String}  data.list.wechatnick           好友昵称
     * @apiSuccess {Number}  data.list.message_cnt          聊天数
     * @apiSuccess {String}  data.list.lats_create_time     最近聊天时间
     */
    public function actionMessageList()
    {
        $params = Yii::$app->request->get();
        $params['wechat_id'] = $params['wechat_id'] ?? '';
        $params['friend_id'] = $params['friend_id'] ?? '';
        $params['keywords'] = $params['keywords'] ?? '';
        $params['date'] = $params['date'] ?? '';
        $params['type'] = $params['type'] ?? '';
        $params['now_date'] = $params['now_date'] ?? '';

//        $page = $params['page'] ?? 1;
//        $pageSize = $params['pageSize'] ?? 10;
        return CommonHelper::dengta_request('/v1/baiyou/wechat-message-list', array_merge($params, ['page' => 0, 'pageSize' => 0]));
    }

    /**
     * @api {get} /v1/customer/wechat/download-message 微信聊天列表
     * @apiName download-message
     * @apiGroup Index
     *
     * @apiParam {String} wechat_id                                 咨询微信ID
     * @apiParam {String} friend_id                                 好友微信ID
     *
     * @apiSuccess {Object}  data                                   返回数据
     */
    public function actionDownloadMessage()
    {
        $params = Yii::$app->request->get();

        $service = new MessageService();
        $service->downloadToPdf($params);
    }

    /**
     * @api {get} /v1/customer/wechat/repair-wechat-message-file-queue       修复微信好友聊天大图
     * @apiName repair-wechat-message-file-queue
     * @apiGroup Site
     *
     * @apiParam {String} start_time                开始时间【格式：2022-01-01 12:00:00】
     * @apiParam {String} end_time                  结束时间【格式：2022-01-01 12:00:00】
     * @apiParam {String} wechatid                  微信ID
     * @apiParam {String} friendid                  好友ID
     *
     * @apiSuccess {Boolen} data                    true or false
     *
     */
    public function actionRepairWechatMessageFileQueue()
    {
        $params = Yii::$app->request->get();
        $params['start_time'] = $params['start_time'] ?? '';
        $params['end_time'] = $params['end_time'] ?? '';
        $params['wechatid'] = $params['wechatid'] ?? '';
        $params['friendid'] = $params['friendid'] ?? '';

        try {
            return CommonHelper::dengta_request('/v1/baiyou/repair-wechat-message-file-queue', $params);
        } catch (\Exception $e) {
            return CommonHelper::api($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @api {get} /v1/customer/wechat/repair-wechat-friend-queue       修复设备好友
     * @apiName repair-wechat-friend-queue
     * @apiGroup Site
     *
     * @apiParam {String} wechatid                  微信id
     *
     * @apiSuccess {Boolen} data                    true or false
     *
     */
    public function actionRepairWechatFriendQueue()
    {
        $params = Yii::$app->request->get();
        $params['wechatid'] = $params['wechatid'] ?? '';

        try {
            return CommonHelper::dengta_request('/v1/baiyou/repair-wechat-friend-queue', $params);
        } catch (\Exception $e) {
            return CommonHelper::api($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @api {post} /v1/customer/wechat/conver-file 转换格式
     * @apiName conver-file
     * @apiGroup Wechat
     *
     * @apiParam {Number} id        消息ID
     *
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionConverFile()
    {
        $params = Yii::$app->request->post();
        $params['id'] = $params['id'] ?? 0;

        try {
            return CommonHelper::dengta_request('/v1/baiyou/conver-file', [], $params);
        } catch (\Exception $e) {
            return CommonHelper::api($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @api {get} /v1/customer/wechat/repair-wechat-message-queue       修复微信好友聊天记录
     * @apiName repair-wechat-message-queue
     * @apiGroup Site
     *
     * @apiParam {String} type                      类型【1项目、2设备下所有好友、3设备下单个好友】
     * @apiParam {String} project_id                项目ID【type=1有效，=0所有项目 >0单个项目】
     * @apiParam {String} wechatid                  微信id
     * @apiParam {String} friendid                  好友id
     * @apiParam {String} start_time                开始时间【格式：2022-01-01 12:00:00】
     * @apiParam {String} end_time                  结束时间【格式：2022-01-01 12:00:00】
     *
     * @apiSuccess {Boolen} data                    true or false
     *
     */
    public function actionRepairWechatMessageQueue()
    {
        $params = Yii::$app->request->get();

        try {
            return CommonHelper::dengta_request('/v1/baiyou/repair-wechat-message-queue', $params);
        } catch (\Exception $e) {
            return CommonHelper::api($e->getCode(), $e->getMessage());
        }
    }

}
